using System;
using System.Data;
using System.IO;

namespace Glpt.Task
{
    /// <summary>
    /// 获取文件路径信息
    /// </summary>
    public class DirPath
    {
        private DataTable dtmap = null;
        /// <summary>
        /// 网盘映射表
        /// </summary>
        public DataTable NetPathMapTable
        {
            get
            {
                return dtmap;
            }
            set
            {
                dtmap = value;
            }
        }
        /// <summary>
        /// 初始化类的新实例
        /// </summary>
        public DirPath()
        {
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="pathname">文件路径</param>
        /// <returns>数据流</returns>
        public DataTableReader SetParams(string pathname)
        {
            return SetParams(pathname, "");
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="pathname">文件路径</param>
        /// <param name="filename">文件名</param>
        /// <returns>数据流</returns>
        public DataTableReader SetParams(string pathname, string filename)
        {
            return SetParams(pathname, filename, "");
        }
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="pathname">文件路径</param>
        /// <param name="filename">文件名</param>
        /// <param name="searchoption">搜索方式 TopDirectoryOnly = 0,AllDirectories = 1</param>
        /// <returns>数据流</returns>
        public DataTableReader SetParams(string pathname, string filename, string searchoption)
        {
            if (filename.Equals("") || filename.Equals("@FILENAME")) filename = "*.*";
            if (searchoption.Equals("") || searchoption.Equals("@SEARCHOPTION")) searchoption = "0";
            DataTable dt = new DataTable();
            dt.Columns.Add("DIRECTORYNAME");
            dt.Columns.Add("FILENAME");
            dt.Columns.Add("LENGTH", typeof(Int64));
            dt.Columns.Add("LASTWRITETIME");
            string pathnew = GetMapPath(pathname, true);
            bool ismap = !pathnew.Equals(pathname);
            if (Directory.Exists(pathnew))
            {
                string[] files = Directory.GetFiles(pathnew, filename, (SearchOption)int.Parse(searchoption));
                foreach (string file in files)
                {
                    FileInfo fi = new FileInfo(file);
                    DataRow dr = dt.NewRow();
                    string tmpname = fi.DirectoryName;
                    if (ismap) tmpname = GetMapPath(tmpname, false);
                    dr[0] = tmpname;
                    dr[1] = fi.Name;
                    dr[2] = fi.Length;
                    dr[3] = fi.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss");
                    dt.Rows.Add(dr);
                }
            }
            return dt.CreateDataReader();
        }

        private string GetMapPath(string path, bool net_to_local)
        {
            if (dtmap == null) return path;
            foreach (DataRow dr in dtmap.Rows)
            {
                string NET_PATH = dr["NET_PATH"].ToString();
                string MAP_PATH = dr["MAP_PATH"].ToString();
                if (net_to_local)
                {
                    if (path.StartsWith(NET_PATH))
                    {
                        path = path.Replace(NET_PATH, MAP_PATH);
                        break;
                    }
                }
                else
                {
                    if (path.StartsWith(MAP_PATH))
                    {
                        path = path.Replace(MAP_PATH, NET_PATH);
                        break;
                    }
                }
            }
            return path;
        }
    }
}
